﻿using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System;
using X.Core.Interfaces;

namespace X.Core.Bases
{
    public abstract class ParameterModelBase : SaveModelBase, IParameterModel
    {
        public ParameterModelBase() : base()
        {

        }
        
        public virtual void SetData(string key, int value)
        {
            if (ParametersInt == null) ParametersInt = new Dictionary<string, int>();
            if (ParametersInt.ContainsKey(key))
                ParametersInt[key] = value;
            else
                ParametersInt.Add(key, value);
        }
        public virtual void SetData(string key, double value)
        {
            if (ParametersDouble == null) ParametersDouble = new Dictionary<string, double>();
            if (ParametersDouble.ContainsKey(key))
                ParametersDouble[key] = value;
            else
                ParametersDouble.Add(key, value);
        }
        public virtual void SetData(string key, bool value)
        {
            if (ParametersBool == null) ParametersBool = new Dictionary<string, bool>();
            if (ParametersBool.ContainsKey(key))
                ParametersBool[key] = value;
            else
                ParametersBool.Add(key, value);
        }
        public virtual void SetData(string key, string value)
        {
            if (ParametersString == null) ParametersString = new Dictionary<string, string>();
            if (ParametersString.ContainsKey(key))
                ParametersString[key] = value;
            else
                ParametersString.Add(key, value);
        }
        public virtual void SetData(string key, Guid value)
        {
            if (ParametersGuid == null) ParametersGuid = new Dictionary<string, Guid>();
            if (ParametersGuid.ContainsKey(key))
                ParametersGuid[key] = value;
            else
                ParametersGuid.Add(key, value);
        }
        public virtual int GetInt(string key, int defaultValue = 0)
        {
            if (ParametersInt == null)
            {
                return defaultValue;
            }
            if(!ParametersInt.ContainsKey(key))
            {
                return defaultValue;
            }
            return ParametersInt[key];
        }
        public virtual double GetDouble(string key, double defaultValue = 0.0)
        {
            if (ParametersDouble == null)
            {
                return defaultValue;
            }
            if (!ParametersDouble.ContainsKey(key))
            {
                return defaultValue;
            }
            return ParametersDouble[key];
        }
        public virtual bool GetBool(string key, bool defaultValue = false)
        {
            if (ParametersBool == null)
            {
                return defaultValue;
            }
            if (!ParametersBool.ContainsKey(key))
            {
                return defaultValue;
            }
            return ParametersBool[key];
        }
        public virtual string GetString(string key, string defaultValue = null)
        {
            if (ParametersString == null)
            {
                return defaultValue;
            }
            if (!ParametersString.ContainsKey(key))
            {
                return defaultValue;
            }
            return ParametersString[key];
        }
        public virtual Guid GetGuid(string key, Guid defaultValue = default(Guid))
        {
            if (ParametersGuid == null)
            {
                return defaultValue;
            }
            if (!ParametersGuid.ContainsKey(key))
            {
                return defaultValue;
            }
            return ParametersGuid[key];
        }
        public virtual void ClearParameters()
        {
            ParametersInt?.Clear();
            ParametersDouble?.Clear();
            ParametersBool?.Clear();
            ParametersString?.Clear();
            ParametersGuid?.Clear();
        }
        #region 'Fields'
        bool _isInitialized = false;
        #endregion

        #region 'Properties'
        public virtual bool IsInitialized { get => _isInitialized; set => SetProperty(ref _isInitialized, value); }
        public JObject JObject { get; set; }
        public Dictionary<string, int> ParametersInt { get; set; }
        public Dictionary<string, double> ParametersDouble { get; set; }
        public Dictionary<string, bool> ParametersBool { get; set; }
        public Dictionary<string, string> ParametersString { get; set; }
        public Dictionary<string, Guid> ParametersGuid { get; set; }
        #endregion

    }
}
